<template>
	<view class="u-page bg-white">
		<u-sticky>
			<!-- <view class="bg-primary p-20 text-center text-white">
				<view>
					<image mode="widthFix" src="/static/image/royal.png" style="width: 60rpx" />
				</view>
				<view>第一名</view>
				<view>本月获得好评：0</view>
			</view> -->
			<view class="flex p-20 u-border-bottom text-center bg-white">
				<view
					v-for="(item, index) in listParam[type - 1]"
					:style="{
						width: index != 1 ? '100rpx' : 'auto',
					}"
					:class="{
						'flex-1': index == 1,
					}"
					:key="`th-${index}`"
					>{{ item.label }}</view
				>
			</view>
		</u-sticky>
		<view v-if="loading" class="pt-30p">
			<u-loading-icon mode="circle" text="加载中..." vertical size="36" textSize="32"></u-loading-icon>
		</view>
		<u-list>
			<u-list-item v-for="(item, index) in list" :key="`order-${item.id}`">
				<view class="flex p-20 u-border-bottom text-center text-sm">
					<view
						v-for="(ite, inde) in listParam[type - 1]"
						:style="{
							width: inde != 1 ? '100rpx' : 'auto',
						}"
						:class="{
							'flex-1': inde == 1,
						}"
						:key="`td-${index}-${inde}`"
						>{{ item[ite.prop] }}</view
					>
				</view>
			</u-list-item>
		</u-list>
	</view>
</template>
<script>
import request from "../../utils/admin-request"
export default {
	data() {
		return {
			type: 1,
			titleList: ["", "商品销量排行榜", "商品评价排行榜"],
			list: [],
			listParam: [
				[
					{ label: "排名", prop: "index" },
					{ label: "商品名称", prop: "goods_name" },
					{ label: "销量", prop: "sales_actual" },
				],
				[
					{ label: "排名", prop: "index" },
					{ label: "商品名称", prop: "goods_name" },
					{ label: "好评", prop: "stars" },
				],
			],
			loading: false,
		}
	},
	onLoad({ type }) {
		if (type) {
			this.type = type
			uni.setNavigationBarTitle({ title: this.titleList[type] })
		}
		this.getList()
	},
	methods: {
		async getList() {
			this.loading = true
			let url = ["", "/api/merch/goodsSaleRankList", "/api/merch/goodsCommentRankList"]
			let { data } = await request({
				url: url[this.type],
			})
			data.forEach((item, index) => {
				item.index = index + 1
			})
			this.loading = false
			this.list = data
		},
	},
}
</script>
